package com.gmd.consultamedica.action;

import java.util.Date;

import com.gmd.consultamedica.service.ConsultaMedicaService;
import com.gmd.historiaclinica.service.HistoClinicaService;
import com.gmd.model.Consultamedica;
import com.gmd.model.Historiaclinica;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class RegConsultaMedicaAction extends ActionSupport implements ModelDriven<Consultamedica>{

	private static final long serialVersionUID = 1L;
	
	private ConsultaMedicaService service;
	private HistoClinicaService serviceHC;
	
	private Consultamedica consultamedica;
	
	public RegConsultaMedicaAction(){
		if(consultamedica==null){
			consultamedica = new Consultamedica();
			//consultamedica.setHistoriaclinica(new Historiaclinica());
		}
		service = new ConsultaMedicaService();
		int id = service.gennerarIdConsMed();
		consultamedica.setIdconsultamedica(id);
		consultamedica.setFechcrea(new Date());
	}
	
	//Methods
	
	public String paciente(){
		String mensaje = "";
		int id = consultamedica.getHistoriaclinica().getIdhistoriaclinica();
		if( id != 0){
			Historiaclinica hc = serviceHC.buscarHistoClinica(id);
			if(hc != null){
				consultamedica.setHistoriaclinica(hc);
				mensaje = "";
			}
		}else{
			mensaje = "No se encontro paciente";
		}
		addActionError(mensaje);
		return "salir";
	}
	
	public String registrar(){
		String retorno = SUCCESS;
		try {
			service.registrarConsMed(consultamedica);
			addActionMessage("La Consulta Medica se genero correctamente");
		} catch (Exception e) {
			addActionMessage(e.getMessage());
			retorno = ERROR;
		}
		return retorno;
	}
	
	//Implements Model Driven
	
	@Override
	public Consultamedica getModel() {
		return consultamedica;
	}
	
	//Getters and Setters
	
	public Consultamedica getConsultamedica() {
		return consultamedica;
	}

	public void setConsultamedica(Consultamedica consultamedica) {
		this.consultamedica = consultamedica;
	}
		
}
